package cn.cs.service.impl;

import cn.cs.client.AgentHandleComplaintClient;
import cn.cs.client.AgentUserClient;
import cn.cs.common.*;
import cn.cs.service.AgentHandleComplaintService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AgentHandleComplaintServiceImpl implements AgentHandleComplaintService{
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private AgentHandleComplaintClient agentHandleComplaintClient;
    @Resource
    private AgentUserClient agentUserClient;

    @Override
    public Dto complaintLsit(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        if(EmptyUtils.isEmpty(agentUser)){
            return DtoUtil.returnFail("用户未登录","001");
        }
        Map<String,Object> user = agentUserClient.getAgentUserByPhone(agentUser.get("phone").toString());
        String state = request.getParameter("state");
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("id",user.get("id").toString());
        param.put("code",user.get("invitation_code").toString());
        param.put("level",user.get("level").toString());
        if(EmptyUtils.isNotEmpty(state)){
            param.put("state",state);
        }
        List<Map<String,Object>> list = agentHandleComplaintClient.getComplaintLsit(param);
        if(EmptyUtils.isNotEmpty(list)){
            return DtoUtil.returnDataSuccess(list);
        }else{
            return DtoUtil.returnFail("没有投诉","002");
        }
    }

    @Override
    public Dto complaintCount(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        if(EmptyUtils.isEmpty(agentUser)){
            return DtoUtil.returnFail("用户未登录","001");
        }
        Map<String,Object> user = agentUserClient.getAgentUserByPhone(agentUser.get("phone").toString());
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("id",user.get("id").toString());
        param.put("code",user.get("invitation_code").toString());
        param.put("level",user.get("level").toString());
        Map<String,Object> countMap = agentHandleComplaintClient.getComplaintCount(param);
        if(EmptyUtils.isNotEmpty(countMap)){
            return DtoUtil.returnDataSuccess(countMap);
        }else{
            return DtoUtil.returnFail("没有查到数量","2");
        }
    }

    @Override
    public Dto getComplaintInfo(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        if(EmptyUtils.isEmpty(agentUser)){
            return DtoUtil.returnFail("用户未登录","001");
        }
        String id = request.getParameter("id");
        Map<String,Object> countMap = agentHandleComplaintClient.getComplaintInfo(id);
        if(EmptyUtils.isNotEmpty(countMap)){
            return DtoUtil.returnDataSuccess(countMap);
        }else{
            return DtoUtil.returnFail("没有这条投诉信息","002");
        }
    }

    @Override
    public Dto handleComplaint(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        String id = request.getParameter("id");
        String content = request.getParameter("content");
        if(EmptyUtils.isEmpty(content)){
            Map<String,Object> map =new HashMap<String,Object>();
            map.put("id",id);map.put("state","1");map.put("knowDate", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
            int n = agentHandleComplaintClient.handleComplaint(map);
            if(n>0){
                return DtoUtil.returnSuccess();
            }else{
                return DtoUtil.returnFail("提交失败","002");
            }
        }else{
            Map<String,Object> map =new HashMap<String,Object>();
            map.put("id",id);map.put("handleContent",content);map.put("state","2");map.put("handleDate", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
            int n = agentHandleComplaintClient.handleComplaint(map);
            if(n>0){
                return DtoUtil.returnSuccess();
            }else{
                return DtoUtil.returnFail("提交失败","002");
            }
        }
    }
}
